package com.bringway.global.util;

import java.io.File;
import java.io.Writer;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.Resource;

public class VelocityUtils {

	
	public static void export(String templateFile, Map parameters, Writer writer) throws Exception {
		VelocityEngine ve = new VelocityEngine();
		
		Properties properties = new Properties();
		
		properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		properties.setProperty("resource.loader", "class");
		properties.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
		
		try {
			ve.init(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Template t = ve.getTemplate(templateFile,"UTF-8");

		VelocityContext context = new VelocityContext();
		for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
			Entry entry = (Entry)iter.next();
			context.put(entry.getKey().toString(),transExcelString(entry.getValue()));
		}

		t.merge(context, writer);
	}
	
	public static void export(Resource templateFile, Map parameters, Writer writer) throws Exception {
		VelocityEngine ve = new VelocityEngine();
		File file = templateFile.getFile();
		String folder= file.getParent();
		String fileName = file.getName();
		Properties properties = new Properties();
		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,folder);
		properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		
		try {
			ve.init(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Template t = ve.getTemplate(fileName,"UTF-8");

		VelocityContext context = new VelocityContext();
		for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
			Entry entry = (Entry)iter.next();
			context.put(entry.getKey().toString(), entry.getValue());
		}

		t.merge(context, writer);
		writer.flush();

	}

	
	/**
	 * 将String中的"\r\n"替换成为"&#10;"
	 * @param parameter
	 * @return
	 */
	protected static Object transExcelString(Object value){
		
			if(value instanceof String){
				String temp = (String)value;
				if(temp!=null){
					temp = temp.replaceAll("&","&amp;" );
					temp =  temp.replaceAll("\n", "&#10;");
					temp = temp.replaceAll("<","&lt;" );
					temp = temp.replaceAll(">","&gt;" );
					temp = temp.replaceAll("\"","&quot;" );
					return temp;
				}
			}
			if(value instanceof Timestamp){
				Timestamp ts = (Timestamp) value;
				return DateUtils.toString(ts, "yyyy-MM-dd HH:mm");
			}
			
			if(value instanceof BigDecimal){
				BigDecimal bd = (BigDecimal) value;
				return bd.toString();
			}
			
			if(value instanceof List) {
				List list = (List) value;
				List res = new ArrayList();
				for (int i = 0; i < list.size(); i++) {
					res.add(transExcelString(list.get(i)));
				}
				return res;
			}
			if(value instanceof Map){
				Map map = (Map) value;
				for (Iterator iterator = map.keySet().iterator(); iterator
						.hasNext();) {
					String key = (String)iterator.next();
					Object v = map.get(key);
					map.put(key, transExcelString(v));
				}
			}
			return value;
	}
	

}
